// 首先引入Mock
import Mock from 'mockjs'
import { responseSuccess, responseFaild } from './libs'
let captureCode = '' // 验证验证码
let userName = '' // 缓存用户的身份
Mock.mock( '/api/auth/login',({ body }) => {
  if (body.code && body.code !== captureCode) {
    return responseFaild({ code: 503, msg: '验证码不正确', data: null })
  }
  userName = body.username
  return responseSuccess({ access_token: 'mock-logined' })
  
})
Mock.mock( '/api/auth/getInfo',({ body }) => {
  const result = responseSuccess()

  return {
    ...result,
    permissions: ['*:*:*'],
    roles: ['admin'],
    user: {
      userId: 1,
      deptId: 45377,
      userName: userName,
      nickName: '管理员',
      email: 'admin@siwei.com',
      phonenumber: '15888888888',
      sex: '0',
      avatar: '',
      status: '0',
      dept: {
        deptId: 45377
      },
      roles: [
        {
          roleId: 1,
          roleName: '超级管理员',
          roleKey: 'admin'
        }
      ]
    }
  }
})
Mock.mock( '/api/auth/getRouters',() => responseSuccess([
  {
    name: 'System',
    path: '/system',
    hidden: true,
    redirect: 'noRedirect',
    component: 'Layout',
    alwaysShow: false,
    meta: {
      title: '系统管理',
      icon: 'system',
      noCache: false,
      link: null
    },
    children: [
      {
        name: 'User',
        path: 'user',
        hidden: false,
        component: 'system/user/index',
        meta: {
          title: '用户管理',
          icon: 'user',
          noCache: false,
          link: null
        }
      },
      {
        name: 'Role',
        path: 'role',
        hidden: false,
        component: 'system/role/index',
        meta: {
          title: '角色管理',
          icon: 'peoples',
          noCache: false,
          link: null
        }
      },
      {
        name: 'Menu',
        path: 'menu',
        hidden: false,
        component: 'system/menu/index',
        meta: {
          title: '菜单管理',
          icon: 'cascader',
          noCache: false,
          link: null
        }
      },
      {
        name: 'Notice',
        path: 'notice',
        hidden: false,
        component: 'system/notice/index',
        meta: {
          title: '通知公告',
          icon: 'message',
          noCache: false,
          link: null
        }
      },
      {
        name: 'Log',
        path: 'log',
        hidden: false,
        redirect: 'noRedirect',
        component: 'ParentView',
        alwaysShow: true,
        meta: {
          title: '日志管理',
          icon: 'log',
          noCache: false,
          link: null
        },
        children: [
          {
            name: 'Operlog',
            path: 'operlog',
            hidden: false,
            component: 'system/operlog/index',
            meta: {
              title: '操作日志',
              icon: 'form',
              noCache: false,
              link: null
            }
          },
          {
            name: 'Logininfor',
            path: 'logininfor',
            hidden: false,
            component: 'system/logininfor/index',
            meta: {
              title: '登录日志',
              icon: 'logininfor',
              noCache: false,
              link: null
            }
          }
        ]
      }
    ]
  }
]))
Mock.mock( '/api/auth/logout',({ body }) => {
  
  return responseSuccess({ access_token: '' })
  
})
export default [
  // 获取验证码
  {
    url: '/api/code',
    method: 'get',
    response: ({ body }) => {
      captureCode = mockjs.Random.word(4)
      return { code: 200, img: Mock.Random.image('200x100', '#e9f0fd', captureCode) }
    }
  },
  // 登陆
  {
    url: '/api/auth/login',
    method: 'post',
    response: ({ body }) => {
      if (body.code && body.code !== captureCode) {
        return responseFaild({ code: 503, msg: '验证码不正确', data: null })
      }
      userName = body.username
      return responseSuccess({ access_token: 'mock-logined' })
    }
  },
  // 用户信息模拟
  {
    url: '/api/auth/getInfo',
    method: 'get',
    response: ({ body }) => {
      const result = responseSuccess()

      return {
        ...result,
        permissions: ['*:*:*'],
        roles: ['admin'],
        user: {
          userId: 1,
          deptId: 45377,
          userName: userName,
          nickName: '管理员',
          email: 'admin@siwei.com',
          phonenumber: '15888888888',
          sex: '0',
          avatar: '',
          status: '0',
          dept: {
            deptId: 45377
          },
          roles: [
            {
              roleId: 1,
              roleName: '超级管理员',
              roleKey: 'admin'
            }
          ]
        }
      }
    }
  },
  // 获取用户权限菜单数据
  {
    url: '/api/auth/getRouters',
    method: 'get',
    response: () => responseSuccess([
        {
          name: 'System',
          path: '/system',
          hidden: true,
          redirect: 'noRedirect',
          component: 'Layout',
          alwaysShow: false,
          meta: {
            title: '系统管理',
            icon: 'system',
            noCache: false,
            link: null
          },
          children: [
            {
              name: 'User',
              path: 'user',
              hidden: false,
              component: 'system/user/index',
              meta: {
                title: '用户管理',
                icon: 'user',
                noCache: false,
                link: null
              }
            },
            {
              name: 'Role',
              path: 'role',
              hidden: false,
              component: 'system/role/index',
              meta: {
                title: '角色管理',
                icon: 'peoples',
                noCache: false,
                link: null
              }
            },
            {
              name: 'Menu',
              path: 'menu',
              hidden: false,
              component: 'system/menu/index',
              meta: {
                title: '菜单管理',
                icon: 'cascader',
                noCache: false,
                link: null
              }
            },
            {
              name: 'Notice',
              path: 'notice',
              hidden: false,
              component: 'system/notice/index',
              meta: {
                title: '通知公告',
                icon: 'message',
                noCache: false,
                link: null
              }
            },
            {
              name: 'Log',
              path: 'log',
              hidden: false,
              redirect: 'noRedirect',
              component: 'ParentView',
              alwaysShow: true,
              meta: {
                title: '日志管理',
                icon: 'log',
                noCache: false,
                link: null
              },
              children: [
                {
                  name: 'Operlog',
                  path: 'operlog',
                  hidden: false,
                  component: 'system/operlog/index',
                  meta: {
                    title: '操作日志',
                    icon: 'form',
                    noCache: false,
                    link: null
                  }
                },
                {
                  name: 'Logininfor',
                  path: 'logininfor',
                  hidden: false,
                  component: 'system/logininfor/index',
                  meta: {
                    title: '登录日志',
                    icon: 'logininfor',
                    noCache: false,
                    link: null
                  }
                }
              ]
            }
          ]
        }
      ])
  }
]
